//
//  StepDiagramTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/21.
//

import UIKit
import FSPagerView

class StepDiagramTableViewCell: UITableViewCell {
    var pagerView: FSPagerView!
    var dataArray: [VideoVRPdfModel] = []
    var pageControl: TYPageControl!
    var page_height: CGFloat = 190
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.selectionStyle = .none
        
        let lang = XTManageCenter.getAppleLanguages()
        if lang.contains("en") {
            self.page_height = 190
        }else{
            self.page_height = 170
        }
        self.initPagerView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initPagerView(){
        
        // Create a pager view
        self.pagerView = FSPagerView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: self.page_height))
        self.pagerView.dataSource = self
        self.pagerView.delegate = self
        self.pagerView.decelerationDistance = 1
        let width = (kScreenWidth - 28)
        self.pagerView.interitemSpacing = 10
        self.pagerView.isInfinite = false//顺时针一直滑动
        self.pagerView.itemSize = CGSize(width: width, height: self.page_height)
        self.pagerView.register(StepDiagramCell.self, forCellWithReuseIdentifier: "StepDiagramCell")
        self.contentView.addSubview(self.pagerView)
        
        
        self.pageControl = TYPageControl(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 4))
        self.pageControl.currentPageIndicatorSize = CGSize(width: 4, height: 4)
        self.pageControl.pageIndicatorSize = CGSize(width: 4, height: 4)
        self.pageControl.backgroundColor = .clear
        self.pageControl.currentPage = 0
        self.pageControl.currentPageIndicatorTintColor = Theme.mainColor
        self.pageControl.pageIndicatorTintColor = UIColor(hex: 0x005C99,alpha: 0.1)//不活跃指示器颜色
        self.pagerView.addSubview(self.pageControl)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        self.pagerView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.greaterThanOrEqualTo(self.page_height)
            make.bottom.equalTo(-10)
        }
        
        
        self.pageControl.snp.makeConstraints { make in
            make.centerX.equalTo(self.pagerView.snp.centerX)
            make.height.equalTo(4)
            make.bottom.equalTo(-10)
        }
        self.layoutIfNeeded()
    }
    
    func setDataArray(dataArray: [VideoVRPdfModel]){
        self.dataArray = dataArray
        if dataArray.count > 1{
            self.pagerView.automaticSlidingInterval = 3
            self.pagerView.isInfinite = true//顺时针一直滑动
        }else{
            self.pagerView.isInfinite = false
        }
        
        self.pageControl.numberOfPages = dataArray.count
        self.pagerView.reloadData()
        self.updateConstraints()
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
}

extension StepDiagramTableViewCell: FSPagerViewDataSource,FSPagerViewDelegate{
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.dataArray.count
        
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "StepDiagramCell", at: index) as! StepDiagramCell
        if self.dataArray.count > index{
            let model = self.dataArray[index]
            cell.setModel(model: model)
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        self.pageControl.currentPage = pagerView.currentIndex
    }
    
    func pagerViewDidEndDecelerating(_ pagerView: FSPagerView){
        self.pageControl.currentPage = pagerView.currentIndex
        print("滑动到了第\(pagerView.currentIndex)个row")
        
    }
    
}
